<?php

namespace app\controller\Admin;

use think\facade\Db;
use think\facade\Request;
use think\Validate;

class ExampleController
{
    public function saveData()
    {
        // 接收表单数据
        $data = Request::post();

        // 数据验证
        $validate = new Validate([
            'name' => 'require|max:50',
            'email' => 'require|email'
        ]);
        if (!$validate->check($data)) {
            return json(['code' => 400, 'msg' => $validate->getError()]);
        }

        // 数据存储
        try {
            $id = Db::table('example_table')->insertGetId($data);
            return json(['code' => 200, 'msg' => '保存成功', 'id' => $id]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => '保存失败：' . $e->getMessage()]);
        }
    }
}
